package top.drinyeit.nongmao.presenter;

import android.content.Intent;

import java.io.Serializable;
import java.util.List;
import java.util.Map;

import top.drinyeit.nongmao.activity.SearchActivity;
import top.drinyeit.nongmao.activity.SearchResultActivity;
import top.drinyeit.nongmao.model.CategoryGoodsDetailsBean;
import top.drinyeit.nongmao.model.CategoryStoreBean;
import top.drinyeit.nongmao.retrofit.HttpResponse;
import top.drinyeit.nongmao.retrofit.RetrofitMethod;
import top.drinyeit.nongmao.retrofit.subscriber.CommonSubscriber;
import top.drinyeit.nongmao.retrofit.subscriber.SubscriberListener;

import static top.drinyeit.nongmao.app.Constant.COMMON_SUCCESS;

/**
 * Created by wu on 2018/1/18.
 */

public class SearchPresenter {
    private SearchActivity activity;

    public SearchPresenter(SearchActivity activity) {
        this.activity = activity;
    }

    //商品搜索
    public void categoryGoodsSearch(final Map<String, Object> map) {
        activity.showLoading();
        CommonSubscriber<HttpResponse<List<CategoryGoodsDetailsBean>>> subscriber = new CommonSubscriber<>(new SubscriberListener<HttpResponse<List<CategoryGoodsDetailsBean>>>() {
            @Override
            public void onError(String e, int code) {
                activity.dismissLoading();
                activity.showToastMsg("错误，请重新操作！");
            }

            @Override
            public void onNext(HttpResponse<List<CategoryGoodsDetailsBean>> o) {
                activity.dismissLoading();
                if (o.getCode() == COMMON_SUCCESS) {
                    Intent intent = new Intent(activity, SearchResultActivity.class);
                    intent.putExtra("search_key", (String) map.get("search_key"));
                    intent.putExtra("type", 0);
                    intent.putExtra("goodsList", (Serializable) o.getData());
                    activity.startActivity(intent);
                } else {
                    activity.showToastMsg(o.getMessage());
                }
            }
        });
        RetrofitMethod.getInstance().categorySearch(subscriber, map);
    }

    //店铺搜索
    public void categoryStoreSearch(final Map<String, Object> map) {
        activity.showLoading();
        CommonSubscriber<HttpResponse<List<CategoryStoreBean>>> subscriber = new CommonSubscriber<>(new SubscriberListener<HttpResponse<List<CategoryStoreBean>>>() {
            @Override
            public void onError(String e, int code) {
                activity.dismissLoading();
                activity.showToastMsg("错误，请重新操作！");
            }

            @Override
            public void onNext(HttpResponse<List<CategoryStoreBean>> o) {
                activity.dismissLoading();
                if (o.getCode() == COMMON_SUCCESS) {
                    Intent intent = new Intent(activity, SearchResultActivity.class);
                    intent.putExtra("search_key", (String) map.get("search_key"));
                    intent.putExtra("type", 1);
                    intent.putExtra("storeList", (Serializable) o.getData());
                    activity.startActivity(intent);
                } else {
                    activity.showToastMsg(o.getMessage());
                }
            }
        });
        RetrofitMethod.getInstance().categoryStoreSearch(subscriber, map);
    }
}
